
/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：历史成交记录                                                    
*│　作    者：gaotang                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-02-13 12:39:31                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： BestTrader.Cms.Services                                  
*│　类    名： History_DealedOrderReportService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using BestTrader.Cms.Core;
using BestTrader.Cms.IRepository;
using BestTrader.Cms.IServices;
using BestTrader.Cms.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BestTrader.Cms.Services
{
    public class History_DealedOrderReportService: IHistory_DealedOrderReportService
    {
        
        private readonly IHistory_DealedOrderReportRepository _repository;
        private readonly IManagerService _managerService;
        private readonly ISysItemsDetailService _itemsService;
        
        public History_DealedOrderReportService(IHistory_DealedOrderReportRepository repository, IManagerService managerService, ISysItemsDetailService itemsService)
        {
            _repository = repository;
            _managerService = managerService;
            _itemsService = itemsService;
        }

        /// <summary>
        /// 首页 交易记录统计
        /// </summary>
        public DealedOrderStatisticsModel GetDealedOrderReportByAgentId(int managerId, DateTime? start, DateTime? end)
        {
            var _managers = _managerService.GetSelfAndChildManagerByManagerIdAndRoleId(managerId.ToString());

            var projects = _itemsService.GetItems(Constants.PROJECT_TYPE);
            var conditions = $"where IsDelete =0 ";
                conditions += $"and DealedDate >= '{ start.Value }' and DealedDate<= '{ end.Value }' ";
            if (_managers.Any()) conditions += $"and AccountName in({ String.Join(',', _managers.Select(p=>$"'{ p.ItemName }','{ string.Format("{0}01", p.ItemName) }'")) })";
            
            var list = _repository.GetList(conditions);
            //var total = _repository.RecordCount(conditions);
            // 总手数
            var total = list.Sum(p => p.Quantity);

            var titles = from order in list
                         group order by order.PfCode into g
                         select $"{g.Key}({projects.FirstOrDefault(p => p.ItemCode == g.Key)?.ItemName})";

            var data = from order in list
                         group order by order.PfCode into g
                         select new DealedOrderChartModel {
                             name = $"{g.Key}({projects.FirstOrDefault(p => p.ItemCode == g.Key)?.ItemName})",
                             value = g.Sum(p=>p.Quantity)
                         };


            var result = new DealedOrderStatisticsModel {
                Total = total,
                ChartTitle = titles,
                ChartData = data
            };

            return result;
        }
    }
}